<?php

include FCPATH . 'application/controllers/backend/backend.php';

class Message extends Backend {

    public function __construct() {
        parent::__construct();
        
        $this->load->model('message_model', 'model');
    }

    public function index() {
        $this->load_javascripts(array('backend_message'));

        $this->rend('message_index_view');
    }

    public function records() {

        $page = intval($this->input->get('page'));

        $page_size = intval($this->input->get('pageSize'));

        $page = $page ? $page : 1;

        $offset = $page_size * ($page - 1);



        $name = trim($this->input->get('name'));

        $telephone = trim($this->input->get('telephone'));

        $email = trim($this->input->get('email'));

        $address = trim($this->input->get('address'));

        $content = trim($this->input->get('content'));



        $conditions = array();

        if (!empty($name)) {
            $conditions[] = array('name like', '%' . $name . '%');
        }

        if (!empty($telephone)) {
            $conditions[] = array('telephone like', '%' . $telephone . '%');
        }

        if (!empty($email)) {
            $conditions[] = array('email like', '%' . $email . '%');
        }

        if (!empty($address)) {
            $conditions[] = array('address like', '%' . $address . '%');
        }

        if (!empty($content)) {
            $conditions[] = array('content like', '%' . $content . '%');
        }



        $this->json_back('XT001', '', array(
            'total' => $this->model->total($conditions),
            'records' => $this->model->records($page_size, $offset, $conditions)
        ));
    }

    public function find() {
        $id = intval($this->uri->segment(4));
        if (($message = $this->model->find($id))) {
            $this->json_back('XT001', '', $message);
        } else {
            $this->json_back('XT002', '该记录已被删除[' . $id . ']');
        }
    }

    public function insert() {
        $data = array(
            'is_display' => 1,
	    'company' => trim($this->input->post('company')),
            'name' => trim($this->input->post('name')),
            'telephone' => trim($this->input->post('telephone')),
            'email' => trim($this->input->post('email')),
            'address' => trim($this->input->post('address')),
            'content' => trim(nl2br($this->input->post('content'))),
            'ip' => $this->input->ip_address(),
            'created_at' => date('Y-m-d H:i:s', time())
        );
        if (empty($data['name'])) {
            $this->json_back('XT002', '请填写您的称呼');
        }
        if (empty($data['telephone'])) {
            $this->json_back('XT002', '请填写您的电话');
        }
        if (empty($data['email'])) {
            $this->json_back('XT002', '请填写您的邮箱');
        }
        if (empty($data['content'])) {

            $this->json_back('XT002', '请填写您的留言内容');
        }
        if (($id = $this->model->insert($data)) > 0) {
            $this->json_back('XT001', '新增留言成功', array('id' => $id));
        } else {
            $this->json_back('XT002', '服务端数据库错误');
        }
    }

    public function update() {
        $id = intval($this->input->post('id'));
        $this->exists($id);
        $data = array(
	    'company' => trim($this->input->post('company')),
            'name' => trim($this->input->post('name')),
            'telephone' => trim($this->input->post('telephone')),
            'email' => trim($this->input->post('email')),
            'address' => trim($this->input->post('address')),
            'content' => trim(nl2br($this->input->post('content'))),
        );
        if (empty($data['name'])) {
            $this->json_back('XT002', '请填写称呼');
        }
        if (empty($data['telephone'])) {
            $this->json_back('XT002', '请填写电话');
        }
        if (empty($data['email'])) {
            $this->json_back('XT002', '请填写邮箱');
        }
        if (empty($data['content'])) {
            $this->json_back('XT002', '请填写留言内容');
        }
        if ($this->model->update($id, $data)) {
            $this->json_back('XT001', '更新留言成功', array('id' => $id));
        } else {
            $this->json_back('XT002', '服务端数据库错误');
        }
    }

    public function hide() {
        $id = intval($this->uri->segment(4));
        $this->exists($id);
        $data = array('is_display' => 0);
        if ($this->model->update($id, $data)) {
            $this->json_back('XT001', '', array('id' => $id));
        } else {
            $this->json_back('XT002', '服务端数据库错误');
        }
    }

    public function delete() {
        $id = intval($this->uri->segment(4));
        $this->exists($id);
        if ($this->model->delete($id)) {
            $this->json_back('XT001', '', array('id' => $id));
        } else {
            $this->json_back('XT002', '服务端数据库错误');
        }
    }

    public function delete_batch() {
        $ids = $this->input->get('ids');
        if ($this->model->delete_batch($ids)) {
            $this->json_back('XT001');
        } else {
            $this->json_back('XT002', '服务端数据库错误');
        }
    }

    private function exists($id) {
        if (!$this->model->find($id)) {
            $this->json_back('XT002', '该记录已被删除[' . $id . ']');
        }
    }

}